package com.cz.nongyetongv5.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.cache.ImageLoader;
import com.cz.nongyetongv5.adapter.config.CommonConstants;
import com.cz.nongyetongv5.bean.RecordHistory;
import com.cz.nongyetongv5.utils.CharsetUtil;

public class RecordHistoryAdapter extends BaseAdapter {

	private Context context;

	private List<RecordHistory> datas = null;

	private boolean mBusy = false;

	public void setFlagBusy(boolean busy) {
		this.mBusy = busy;
	}

	private ImageLoader mImageLoader;

	public RecordHistoryAdapter(Context context, List<RecordHistory> datas) {
		this.context = context;
		this.datas = datas;

		mImageLoader = new ImageLoader(context);
	}

	@Override
	public int getCount() {
		return datas == null ? 0 : datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {

		if (convertView == null) {

			convertView = LayoutInflater.from(context).inflate(
					R.layout.record_history_item, null);

			ViewHolder holder = new ViewHolder();

			holder.tvBeiJianName = (TextView) convertView
					.findViewById(R.id.tvBeiJianName);

			holder.tvJianGuanName = (TextView) convertView
					.findViewById(R.id.tvJianGuanName);

			holder.tvRenYuan = (TextView) convertView
					.findViewById(R.id.tvRenYuan);

			holder.tvChanPin = (TextView) convertView
					.findViewById(R.id.tvChanPin);

			holder.tvResult = (TextView) convertView
					.findViewById(R.id.tvResult);

			holder.tvDate = (TextView) convertView.findViewById(R.id.tvDate);

			holder.ivImage = (ImageView) convertView.findViewById(R.id.ivImage);

			convertView.setTag(holder);
		}

		ViewHolder holder = (ViewHolder) convertView.getTag();

		RecordHistory history = (RecordHistory) getItem(position);

		// 被检单位
		holder.tvBeiJianName.setText(history.getBeiJianName());

		// 产品检测
		if (CharsetUtil.isNotNull(history.getChanPin())) {

			holder.tvJianGuanName
					.setText(Html
							.fromHtml("<b><font color=\"#36495C\">抽验机构:&nbsp;</font></b>"
									+ history.getJianGuanName()));

			holder.tvRenYuan
					.setText(Html
							.fromHtml("<b><font color=\"#36495C\">抽样人员:&nbsp;</font></b>"
									+ history.getRenYuan()));

			holder.tvChanPin.setVisibility(View.VISIBLE);

			holder.tvChanPin
					.setText(Html
							.fromHtml("<b><font color=\"#36495C\">检验产品:&nbsp;</font></b>"
									+ history.getChanPin()));

		} else {

			holder.tvJianGuanName
					.setText(Html
							.fromHtml("<b><font color=\"#36495C\">检查单位:&nbsp;</font></b>"
									+ history.getJianGuanName()));

			holder.tvRenYuan
					.setText(Html
							.fromHtml("<b><font color=\"#36495C\">检查人员:&nbsp;</font></b>"
									+ history.getRenYuan()));

			// 监督检查
			holder.tvChanPin.setVisibility(View.GONE);

			holder.tvChanPin.setText("");
		}

		holder.tvResult.setText(history.getResultString());



		holder.tvResult
				.setBackgroundColor(Color.parseColor(history.getColor()));

		holder.tvDate.setText(history.getTime());



		if (CharsetUtil.isNotNull(history.getImageUrl())) {

			holder.ivImage.setVisibility(View.VISIBLE);

			if (!mBusy) {
				mImageLoader.DisplayImage(history.getImageUrl(),
						holder.ivImage, false);
			} else {
				mImageLoader.DisplayImage(history.getImageUrl(),
						holder.ivImage, true);
			}

		} else {
			holder.ivImage.setVisibility(View.GONE);
		}

		return convertView;
	}

	class ViewHolder {

		public TextView tvBeiJianName;

		public TextView tvJianGuanName;

		public TextView tvRenYuan;

		public TextView tvChanPin;

		public TextView tvResult;

		public TextView tvDate;

		public ImageView ivImage;

	}

	public ImageLoader getmImageLoader() {
		return mImageLoader;
	}
}
